package gold.contest;

import gold.utils.InputUtil;

import java.util.TreeMap;
import java.util.TreeSet;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC6056 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String largestGoodInteger(String num) {
            TreeSet<Integer> record = new TreeSet<>();
            int i = 0, len = num.length();
            while (i < len) {
                int curNum = num.charAt(i) - '0';
                if (record.contains(curNum) || i >= len - 2 || !(num.charAt(i) == num.charAt(i + 1) && num.charAt(i) == num.charAt(i + 2))) {
                    i++;
                    continue;
                }
                record.add(curNum);
                int j = i + 3;
                while (j < len && num.charAt(i) == num.charAt(j)) j++;
                i = j;
            }

            if (record.isEmpty()) return "";
            int ch = record.last();
            String ans = "";
            for (int j = 0; j < 3; j++) {
                ans = ans + ch;
            }
            return ans;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.largestGoodInteger("6777133339"));
        System.out.println(solution.largestGoodInteger("2300019"));
        System.out.println(solution.largestGoodInteger("42352338"));
    }

    public static void main(String[] args) throws Exception {
        LC6056 an = new LC6056();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
